package com.example.app_emu.core

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.widget.Toast
import java.net.DatagramSocket

class CommunicationService: Service() {

    override fun onCreate() {
        super.onCreate()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel("emu_notify", "模拟器服务启动", NotificationManager.IMPORTANCE_HIGH)
            val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            manager.createNotificationChannel(channel)
            val notification = Notification.Builder(applicationContext, "emu_notify").build()
            startForeground(0x555, notification)
        }
    }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        super.onStartCommand(intent, flags, startId)

        val context = this.baseContext
        if (context != null) {
            EmulatorManage.startEmulator(context)
            Toast.makeText(applicationContext, "模拟器通信服务启动", Toast.LENGTH_SHORT).show()
        }

        return START_STICKY
    }

    override fun onDestroy() {
        super.onDestroy()
        EmulatorManage.stopEmulator()
        Toast.makeText(applicationContext, "模拟器通信服务关闭", Toast.LENGTH_SHORT).show()
    }

    override fun onBind(intent: Intent): IBinder? {
        return null
    }
}